// pages/find/find.js
const app = getApp()
let leftHeight = 0,
  rightHeight = 0; //分别定义左右两边的高度
let query;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasLogin:false,
    scrollTop: 0, //距离顶部
    active: 0, //tab下标
    bgImg: [{
      url: '',
      img: 'http://m.qpic.cn/psc?/V11Sie321T6lY9/OmVq1gFIObiZCCY30a7VNzuyT5jAx8bCtJj6aMmOFHXVkjXeGUIX7grl8Uy1yBe2WMtELOghbVfUTR9ZZ4KO*A!!/b&bo=HQTEAR0ExAEDCSw!&rf=viewer_4'
    }, {
      url: '',
      img: 'http://m.qpic.cn/psc?/V11Sie321T6lY9/OmVq1gFIObiZCCY30a7VNzuyT5jAx8bCtJj6aMmOFHXVkjXeGUIX7grl8Uy1yBe2WMtELOghbVfUTR9ZZ4KO*A!!/b&bo=HQTEAR0ExAEDCSw!&rf=viewer_4'
    }, {
      url: '',
      img: 'http://m.qpic.cn/psc?/V11Sie321T6lY9/OmVq1gFIObiZCCY30a7VN9BFlVuls8I3UYHOTLJFc.mYyAhWl3r7p6hwyA.EaH9mUQvE9tm7OHsEm7AliSwz.Q!!/b&bo=HQQsAR0ELAEDCSw!&rf=viewer_4'
    }],
    cur: 0, //轮播激活

    // 推荐
    pageSize: 8,
    pageNum: 1,
    //好友
    pageSizeF: 4,
    pageNumF: 1,
    FensDynamic: [], //获取关注列表好友动态
    // 瀑布流
    list: [],
    leftList: [],
    rightList: [],

  },


  // 轮播
  swiperChange: function (e) {
    this.setData({
      cur: e.detail.current
    })
  },
  //  右侧滚动触发
  bindscrollTop(event) {

  },
  //回到顶部
  goTop: function (e) { // 一键回到顶部
    console.log(1111);
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
  },
  // 发布动态 pages/find/releaseNews/releaseNews  
  releaseNews(e) {
    if (app.checkHasLogin()) {
      wx.navigateTo({
        url: '/pages/find/releaseNews/releaseNews?type=' + 3,
      })
    }

  },
  // tab  切换
  onChangetab(event) {
    // console.log(event.detail.name);
    let active = event.detail.name
    this.setData({
      active: active
    })
    if (active == 0) { //推荐
      this.setData({
        list: [],
        leftList: [],
        rightList: [],
        pageSize: 8,
        pageNum: 1,
      })
      this.getAllList()
    } else if (active == 1) { //好友动态
      if (app.checkHasLogin()) {
        this.getFensDynamic()
      } else {
        this.setData({
          active: 0
        })
      }
    }
  },

  // 瀑布流
  async isLeft() {
    const {
      list,
      leftList,
      rightList
    } = this.data;
    query = wx.createSelectorQuery();
    for (const item of list) {
      leftHeight <= rightHeight ? leftList.push(item) : rightList.push(item); //判断两边高度，来觉得添加到那边
      await this.getBoxHeight(leftList, rightList);
    }
  },
  getBoxHeight(leftList, rightList) { //获取左右两边高度
    return new Promise((resolve, reject) => {
      this.setData({
        leftList,
        rightList
      }, () => {
        query.select('#left').boundingClientRect();
        query.select('#right').boundingClientRect();
        query.exec((res) => {
          leftHeight = res[0].height; //获取左边列表的高度
          rightHeight = res[1].height; //获取右边列表的高度
          resolve();
        });
      });
    })
  },
  //跳转
  navigateTo(e) {
    const data = e.currentTarget.dataset.data //跳转参数
    const path = e.currentTarget.dataset.path

    wx.navigateTo({
      url: path,
    })
  },

  // GET/mobileUser/dynamic/getAllList查询所有人动态列表
  getAllList() {
    wx.showLoading({
      title: '加载中...',
      success: () => {}
    })

    var postObj = {
      url: '/mobileUser/dynamic/getAllList',
      method: 'get',
      data: {
        pageSize: this.data.pageSize,
        pageNum: this.data.pageNum,
      }
    }
    app.request.loginRequest(postObj).then(res => {
      wx.hideLoading({
        complete: (res) => {},
      })
      if (res.data.code === 200) {
        wx.stopPullDownRefresh() //停止下拉刷新
        var rows = res.data.rows
        console.log('查询所有人动态列表', rows);
        if (rows.length > 0) {
          rows.forEach((items, i) => {
            items.sysMobileUser.image = app.util.config().api + items.sysMobileUser.image
            if (items.dynamicFileList.length > 0) {
              items.dynamicFileList.forEach((items1, i) => {
                items1.url = app.util.config().api + items1.url
              })
            }
          })
        }
        // var list = [...this.data.list, ...rows]
        var list = [...rows]
        this.setData({
          list: list
        })
        // console.log('获取瞬间列表', this.data.list);

        if (rows.length > 0) {
          this.isLeft()
        }
      }
    })
  },
  //GET/mobileUser/follow/getFensDynamic获取关注列表好友动态
  getFensDynamic() {
    wx.showLoading({
      title: '加载中...',
      success: () => {}
    })

    var postObj = {
      url: '/mobileUser/follow/getFensDynamic',
      method: 'get',
      data: {
        pageSize: this.data.pageSizeF,
        pageNum: this.data.pageNumF,
      }
    }
    app.request.wxRequest(postObj).then(res => {
      wx.hideLoading({
        complete: (res) => {},
      })
      if (res.data.code === 200) {
        wx.stopPullDownRefresh() //停止下拉刷新
        var rows = res.data.data

        if (rows.length > 0) {
          rows.forEach((items, i) => {
            items.sysMobileUser.image = app.util.config().api + items.sysMobileUser.image
            if (items.dynamicFileList.length > 0) {
              items.dynamicFileList.forEach((items1, i) => {
                items1.url = app.util.config().api + items1.url
              })
            }
          })
        }
        var list = [...rows]
        this.setData({
          FensDynamic: list
        })
        console.log('获取关注列表好友动态', list);

      }
    })
  },
  // 跳转到动态详情
  navigateTodynamicDetail(e) {
    const dynamicId = e.currentTarget.dataset.dynamicid //跳转参数
    // const path = e.currentTarget.dataset.path

    wx.navigateTo({
      url: `/pages/find/dynamicDetail/dynamicDetail?dynamicId=${dynamicId}`
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.getAllList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    // that.getAllList()
    // that.isLeft()

    // if (that.data.active == 0) { //推荐
    //   that.setData({
    //     list: [],
    //     leftList: [],
    //     rightList: [],
    //     pageSize:8,
    //     pageNum: 1,
    //   })
    //   that.getAllList()
    // }

    if (app.globalData.hasLogin==true){
      that.setData({
        hasLogin:true
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this
    if (that.data.active == 0) { //推荐
      that.setData({
        list: [],
        leftList: [],
        rightList: [],
        pageSize: 8,
        pageNum: 1,
      })
      this.getAllList()
    }else if (that.data.active == 1) {//好友
      that.setData({
        FensDynamic:[]
      })
      that.getFensDynamic()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this

    if (that.data.active == 0) { //推荐
      that.setData({
        pageSize: 8,
        pageNum: that.data.pageNum + 1,
      })
      this.getAllList()

    }






  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})